草庐IT

Ruby File 类和方法

全部标签

pointers - 为什么指针上的方法在指针是变量时起作用,而在其他情况下不起作用?

运行以下代码时:packagemainimport("fmt")typeBarstruct{namestring}func(fooBar)testFunc(){fmt.Println(foo.name)}funcdoTest(pointer*Bar){pointer.testFunc()//run`testFunc`onthepointer(eventhoughitexpectsavalueoftype`Bar`,not`*Bar`)}funcmain(){varbazBar=Bar{name:"JohnnyAppleseed",}doTest(&baz)//sendapointero

GO - 方法重新声明错误

规则是,方法只能在命名类型和指向命名类型的指针上定义。对于以下code,packagemaintypeCatstruct{}func(cCat)foo(){//dostuff_}func(c*Cat)foo(){//dostuff_}funcmain(){}编译器报错:main.go:10:methodredeclared:Cat.foomethod(Cat)func()method(*Cat)func()以上代码定义,方法foo()用于命名类型(Cat)和方法foo()用于指向命名类型(*Cat)的指针。问题:对于GO编译器,为什么要考虑为不同类型定义的方法一样吗?

go - 压缩存储在 big.Int 中的余额的最佳方法是什么?

我需要将以太坊(加密货币)余额导出到Postgres,但我需要将它们压缩成一个blob,因为它们太多了,我必须为每个block存储状态。余额存储在big.Int中,但大多数帐户的余额为0(或非常接近于0),所以我想到了这种压缩算法:Format(singlerecord):8bits:thelengthofthebitstringfollowingbits:theactualbig.IntconvertedintobitswithInt.Bits()function余额以1/10^18的精度存储,因此1个以太币存储为1位和18个零。我的算法是最好的压缩方法吗?或者有更好的主意吗?例如,

go - 在 Go 语言中返回其结构地址的方法

在尝试创建链表数据结构时,我声明了两个结构。packagemainimport"fmt"typelistElementstruct{dataintnext*listElement}typeListstruct{first*listElementlast*listElementlenint}我想创建一个返回空列表的方法。到目前为止,我只能创建函数funcnew()*List{return&List{}}这与我在多个数据结构存储库中看到的结果相同。是否可以创建一个方法List.new()来返回一个新的空列表? 最佳答案 是的,您当然可以

go - 接收方是否确定应用哪种方法?

围棋之旅:https://tour.golang.org/methods/9packagemainimport("fmt""math")typeAbserinterface{Abs()float64}funcmain(){varaAbserf:=MyFloat(-math.Sqrt2)v:=Vertex{3,4}a=f//aMyFloatimplementsAbsera=&v//a*VerteximplementsAbser//Inthefollowingline,visaVertex(not*Vertex)//anddoesNOTimplementAbser.a=vfmt.Print

go - 如何仅使用一种方法删除不同类型的 slice

我有2个函数,如下所示funcremoveL2McEntry(a[]api.L2McEntry,indexint)[]api.L2McEntry{a=append(a[:index],a[index+1:]...)elementreturna[:len(a)]}funcremoveVlagBasedGroup(a[]api.VlanPortBased,indexint)[]api.VlanPortBased{a=append(a[:index],a[index+1:]...)returna[:len(a)]}如您所见,这两个函数都在做同样的工作。但我需要将它们分开,因为函数的输出和输入

go - 有什么方法可以使用 golang std 库来格式化日志(如下所述)?

2018年2月1日下午3:04:05(UTC)|这是日志消息2018-02-01T15:04:05Z|这是日志消息我在gosrc中找到了下一个格式:https://github.com/golang/go/blob/master/src/log/log.go#L37但似乎我无法仅使用那些来做到这一点.. 最佳答案 这里有两个选项:使用log.SetOutput设置自定义编写器。或者使用fmt包而不是只打印到stdout或其他地方-stdlib日志包做的不多而且很容易创建你自己的日志包,它以自定义时间格式输出到stdout(或日志文件

go - 如何为以文件路径作为参数的golang方法提供Windows完整路径

所有golang方法都说ioutil.ReadFile理解unix路径,但它不采用windows路径。有没有办法以优雅的方式实现这一点,以便这些方法可以同时采用unix和windows路径。 最佳答案 您也可以在Windows中使用“/”。示例代码如下。packagemainimport("fmt""io/ioutil""log")funcmain(){content,err:=ioutil.ReadFile("D:/temp/main.go")iferr!=nil{log.Fatal(err)}fmt.Printf("Fileco

Golang 接口(interface)方法链接

我有一个接口(interface)Cells有几个方法typeCellsinterface{Len()int//....}具体实现有StrCells、IntCells、FloatCells和BoolCells,它们都有上面的实现的方法。例如:typeStrCells[]stringfunc(sCStrCells)Len()int{returnlen(sC)}//...typeIntCells[]intfunc(iCIntCells)Len()int{returnlen(iC)}//...//....对于两种具体类型-IntCells和FloatCells-我想实现仅适用于这些类型的特定

Go Web 服务 - 未定义的类型没有字段或方法

这个问题在这里已经有了答案:Functioninsamepackageundefined(10个答案)关闭8个月前。我正在尝试在Web服务中整合路由功能。包main有两个值得关注的文件,route.go和main.go。在route.go中,我定义路由如下:packagemainimport("github.com/justinas/alice""net/http")func(app*Application)Routes()http.Handler{standardMiddleware:=alice.New(app.logRequest)mux:=http.NewServeMux()m